package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.base.BaseEntity;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService {
    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.selectByCategoryId(category1Id, category2Id, category3Id);

    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //判断传入的attrInfo是否有id，如果有，则为更新操作，否则为新增操作
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if (baseAttrInfo.getId() == null){
            //新增操作
            baseAttrInfoMapper.insert(baseAttrInfo);
            attrValueList.forEach(baseAttrValue -> {
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(baseAttrValue);
            });
        }else {
            //更新操作
            //先拿到传过来的attrValue的id列表,跟库中的id列表对比,少的直接删除,多的直接更新

            List<Long> valueIdList = attrValueList.stream().map(BaseEntity::getId).collect(Collectors.toList());
            List<Long> dbValueIdList =baseAttrValueMapper.getIdsByAttrId(baseAttrInfo.getId());
            dbValueIdList.forEach(dbValueId -> {
                if (!valueIdList.contains(dbValueId)){
                    //删除操作
                    baseAttrValueMapper.deleteById(dbValueId);
                }else {
                    //更新操作
                    attrValueList.forEach(baseAttrValue -> {
                        if (Objects.equals(baseAttrValue.getId(), dbValueId)){
                            baseAttrValueMapper.updateById(baseAttrValue);
                        }
                    });
                }
            });
            //新增操作,valueIdList中有而dbValueIdList中没有的,则新增
            valueIdList.forEach(valueId->{
                if (!dbValueIdList.contains(valueId)){
                    //新增操作
                    attrValueList.forEach(baseAttrValue -> {
                        if (Objects.equals(baseAttrValue.getId(), valueId)){
                            baseAttrValue.setAttrId(baseAttrInfo.getId());
                            baseAttrValueMapper.insert(baseAttrValue);
                        }
                    });
                }
            });
        }


    }
}




